<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 * @property PaginatorComponent $Paginator
 */
class UsersController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->User->exists($id)) {
			throw new NotFoundException(__('Invalid user'));
		}
		$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
		$this->set('user', $this->User->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
   public function admin_add() {
		if ($this->request->is('post')) {
			$this->User->create();
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved.'),'backend/green',array('redirect'=>''));
				return $this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'),'red',array('redirect'=>''));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->User->exists($id)) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->User->saveAssociated($this->request->data,array('deep'=>true,'atomic'=>true))) {
				$this->Session->setFlash(__('The user has been saved.'),'yellow',array('redirect' => "123"));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'),'red',array('redirect'=>'loi'));
			}
		} else {
			$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
			$this->request->data = $this->User->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		//$this->request->onlyAllow('post', 'delete');
		if ($this->User->delete()) {
			$this->Session->setFlash(__('The user has been deleted.'),'backend/green',array('redirect'=>'index'));
		} else {
			$this->Session->setFlash(__('The user could not be deleted. Please, try again.'),'backend/red',array('redirect'=>'123'));
		}
		return $this->redirect(array('action' => 'admin_index'));
	}
    public function admin_login()
    {
        $this->layout="backend/login";
         $data = $this->request->data;
        if(!empty($data))
        {
            $uname = $data['User']['Username'];
            $pass  = $data['User']['Password'];
            $users = $this->User->find('first',array('conditions'=>array('User.username'=>$uname,'User.password'=>$pass,'User.deleflg <>'=>1)));
            if(count($users) >0)
            {
                $idUser    = $users['User']['id'];
                $lastLogin = date("Y-m-d H:i:s");
                $numLogin  = intval($users['User']['numlogin']) + 1;
                $ipLogin   = $_SERVER["REMOTE_ADDR"];
                if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
                    $ipLogin = $_SERVER['HTTP_CLIENT_IP'];
                } else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                    $ipLogin = $_SERVER['HTTP_X_FORWARDED_FOR'];
                }
                $this->Session->write('userId',$idUser);
                $this->Session->write('userName',$uname);
                $this->Session->write('loginTime',$lastLogin);
                $this->User->id = $idUser;
                $this->User->set('lastlogin',$lastLogin);
                $this->User->set('numlogin',$numLogin);
                $this->User->set('islogin',1);
                $this->User->set('iplogin',$ipLogin);
                $this->User->save();
                $this->redirect("/Pages/admin_index");
                //$this->Session->setFlash('Login successfull ','',array('class'=>false));
            }
            else {
                $this->Session->setFlash('Username hoặc password không đúng ','',array('class'=>false));
            }
        }
        else{
            $this->Session->setFlash('Nhập thông tin đăng nhập','',array('class'=>false));
        }       
    }
    public function admin_index() {
	    $this->User->recursive=0;
        $this->paginate=array('limit'=>10);
		$users =$this->paginate('User');
        //$log = $this->User->getDataSource()->getLog(false, false);
        //debug($log);exit;
        $this->set(compact('users'));
	}
    public function admin_logout()
    {
        $this->Session->destroy();
        $this->redirect("/");
    }
    
 }
